package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntityFoodstuffInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.food.GetFoodstuffListRequest;
import com.ocom.common.response.payconfig.OrderSummaryVo;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoFoodstuffInfo;
import com.ocom.payconfig.service.FoodstuffInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FoodstuffInfoServiceImpl extends ServiceImpl<IDaoFoodstuffInfo, EntityFoodstuffInfo> implements FoodstuffInfoService {

    @Autowired
    IDaoFoodstuffInfo iDaoFoodstuffInfo;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Integer getOnline(Long companyId, Integer merchId, Integer foodClassId) {
        QueryWrapper<EntityFoodstuffInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", merchId);
        queryWrapper.eq("food_class_id", foodClassId);
        return this.count(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public PageInfo<EntityFoodstuffInfo> getList(Long companyId, GetFoodstuffListRequest request) {
        QueryWrapper<EntityFoodstuffInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());

        if(!CommonUtil.isNull(request.getFoodstuffName())){
            queryWrapper.like("food_class_name",request.getFoodstuffName());
        }
        if(!CommonUtil.isNull(request.getClassIds())){
            List<Integer> list = Arrays.asList(request.getClassIds().split(",")).stream().map(x->Integer.parseInt(x)).collect(Collectors.toList());
            queryWrapper.in("food_class_id",list);
        }
        PageHelper.startPage(request.getPage(), request.getSize());
        List<EntityFoodstuffInfo>  foodstuffInfos = this.list(queryWrapper);
        PageInfo<EntityFoodstuffInfo> pageInfo=new PageInfo<>(foodstuffInfos);
        return pageInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Integer getSameNameCode(Long companyId, Integer merchId, String foodstuffCode, String foodstuffName, Boolean isUpdate, Integer foodstuffId) {
        QueryWrapper<EntityFoodstuffInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", merchId);
        if(isUpdate){
            queryWrapper.ne("foodstuff_id", foodstuffId);
        }
        queryWrapper.and(wrapper -> {
            wrapper.or().eq("foodstuff_name",foodstuffName).or().eq("foodstuff_code",foodstuffCode);
        });
        return this.count(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean saveOne(Long companyId, EntityFoodstuffInfo entityFoodstuffInfo) {
        return this.save(entityFoodstuffInfo);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Integer getMaxSort(Long companyId, EntityFoodstuffInfo entityFoodstuffInfo) {
        QueryWrapper<EntityFoodstuffInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", entityFoodstuffInfo.getMerchId());
        queryWrapper.eq("food_class_id", entityFoodstuffInfo.getFoodClassId());
        queryWrapper.orderByDesc("sort");
        queryWrapper.last("limit 1");
        EntityFoodstuffInfo entityFoodstuffInfo1=  this.getOne(queryWrapper);
        if(entityFoodstuffInfo1==null){
            return  10;
        }else{
            return  entityFoodstuffInfo1.getSort()+10;
        }
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateOne(Long companyId, EntityFoodstuffInfo entityFoodstuffInfo) {
        UpdateWrapper<EntityFoodstuffInfo> updateWrapper  = new UpdateWrapper<>();
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("merch_id", entityFoodstuffInfo.getMerchId());
        updateWrapper.eq("foodstuff_id", entityFoodstuffInfo.getFoodstuffId());

        updateWrapper.set("food_class_id", entityFoodstuffInfo.getFoodClassId());
        updateWrapper.set("foodstuff_name", entityFoodstuffInfo.getFoodstuffName());
        updateWrapper.set("foodstuff_code", entityFoodstuffInfo.getFoodstuffCode());
        updateWrapper.set("sort", entityFoodstuffInfo.getSort());
        updateWrapper.set("remark", entityFoodstuffInfo.getRemark());

        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<OrderSummaryVo.FoodstuffList> getFoodstuffList(Long companyId, List<Integer> foodIds) {
        return iDaoFoodstuffInfo.getFoodstuffList(companyId,foodIds);
    }


}
